<template>
  <div class="batch-btns">
    <a-popover v-model:open="popoverVisible" trigger="click">
      <template #content>
        <div style="cursor: pointer;" @click=" popoverVisible = false">
          <p @click="instorageVisible = true">
            导入入库流水
          </p>
          <p>导出excel</p>
        </div>
      </template>
      <div class="choose-btn">
        <span>数据导入导出</span>
        <icon-font type="icon-yumengtubiao_jiantouxiangxia" style="margin-left: 10px; color: #7d8389;" />
      </div>
    </a-popover>
    <a-modal
      v-model:open="instorageVisible" title="导入入库流水" width="520px" :footer="null" destroy-on-close
      @cancel="closeImport"
    >
      <ImportExcel :excel="excelObj" @flush="flushTable" />
    </a-modal>
  </div>
</template>

<script setup>
import ImportExcel from '@/components/ImportExcel/index.vue'
import { Modal } from 'ant-design-vue'

const emit = defineEmits(['refresh'])

const instorageVisible = ref(false) // 导入显示
const popoverVisible = ref(false)
const refresh = reactive({ flush: false, failExcel: '' })

const excelObj = {
  fileName: '导入入库流水.xlsx',
  downLoadUrl: 'http://192.168.1.99:9000/bee-mall/过磅明细模板.xlsx',
  api: '/admin/instorage/import',
  params: {},
}

function flushTable(excelUrl) {
  refresh.flush = true
  refresh.failExcel = excelUrl
}

function closeImport() {
  if (refresh.failExcel) {
    Modal.confirm({
      title: '失败数据下载提示',
      okText: '下载',
      content: '您有导入失败数据未下载，请点击下载',
      onOk() {
        window.location.href = refresh.failExcel
        emit('refresh')
        return Promise.resolve()
      },
      onCancel() {
        emit('refresh')
      },
    })
  }
  if (refresh.flush && refresh.failExcel == '') {
    emit('refresh')
  }
}
</script>

<style lang="less" scoped>
.batch-btns {
  margin-right: 10px;

  .choose-btn {
    height: 30px;
    padding: 0 8px;
    font-size: 14px;
    line-height: 30px;
    color: #575757;
    cursor: pointer;
    background: #f2f5f7;
    border-radius: 4px;
  }
}
</style>
